package com.itic.market.mobile.uc;

import java.text.NumberFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.DateHelper;
import com.itic.appbase.framework.utils.NumberStringHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.market.mobile.base.BaseMobileInterface;
import com.itic.market.system.premoney.persistence.model.PreMoney;
import com.itic.market.system.premoney.persistence.model.PreMoneyExample;
import com.itic.market.system.premoney.service.PreMoneyService;
import com.itic.market.system.profitmoney.persistence.model.ProfitMoney;
import com.itic.market.system.profitmoney.persistence.model.ProfitMoneyExample;
import com.itic.market.system.profitmoney.persistence.model.ProfitMoneyExample.Criteria;
import com.itic.market.system.profitmoney.service.ProfitMoneyService;
import com.itic.market.system.user.persistence.model.MarketUser;


/**
 * 用户登入接口类
 * @author 许小宝
 * 2017年07月25日  14时11分
 */
@Controller
@RequestMapping(value="/mobile/uc")
public class MarketUcInterface
     extends BaseMobileInterface {
    
	@Autowired
	private ProfitMoneyService profitMoneyService;
	@Autowired
	private PreMoneyService preMoneyService;
    /**
     * APP用户注册成商家
     * @return
     */
    @RequestMapping(value="/index")
    @ResponseBody
    public Map<String,Object> index(HttpServletRequest request,HttpServletResponse response,String token){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(token) ){
    			MarketUser user = getMarketUserByToken(token);
    			if( null != user ){
    				if(StringHelper.isBlank(user.getUserHeadImg())){
    					user.setUserHeadImg("http://file.jhjvip.cn/images/default.jpg");
    				}
    				resultMap.put(DATA, user);
    				result = SUCCESS;
    				errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG;
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    /**
     * 收入明细
     * @return
     */
    @RequestMapping(value="/tgmx")
    @ResponseBody
    public Map<String,Object> tgmx( HttpServletRequest request,HttpServletResponse response, String token, String orderNum, String orderType, String startTime, String endTime ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(token) ){
    			MarketUser user = getMarketUserByToken(token);
    			if( null != user ){
    				Map<String, Object> dataMap = new HashMap<String,Object>();
    				
    				ProfitMoneyExample example = new ProfitMoneyExample();
    				example.setOrderByClause("CREATE_TIME DESC");
    				Criteria criteria = example.createCriteria();
    				criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    				criteria.andMarketUserIdEqualTo(user.getId());
    				
    				if( StringHelper.isNotBlank(orderType) ){
    					criteria.andTypeEqualTo(orderType);
    				}
    				if( StringHelper.isNotBlank(orderNum) ){
    					criteria.andOrderNumLike("%"+orderNum+"%");
    				}
    				
    				if( StringHelper.isNotBlank(startTime) ){
    					Date dateByStringDate = DateHelper.getDateByStringDate(startTime+" 00:00:00");
    					criteria.andCreateTimeGreaterThanOrEqualTo( dateByStringDate );
    				}
    				
    				if( StringHelper.isNotBlank(endTime) ){
    					Date dateByStringDate = DateHelper.getDateByStringDate(endTime+" 23:59:59");
    					criteria.andCreateTimeLessThanOrEqualTo(dateByStringDate);
    				}
    				
    				example = this.handlePageQuery(request, example);
    				int count = profitMoneyService.count(example);
    				List<ProfitMoney> list = profitMoneyService.list(example);
    				
    				dataMap.put("orders", list);
    				dataMap.put("count", count);
    				dataMap.put("money", user.getTotalProfit());                                                                                                                                                                                                                                                                                                             
    				resultMap.put(DATA, dataMap);
    				result = SUCCESS;
					errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG;
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    
    /**
     * 收益明细
     * @return
     */
    @RequestMapping(value="/mjgl")
    @ResponseBody
    public Map<String,Object> mjgl( HttpServletRequest request,HttpServletResponse response, String token, String name, String startTime, String endTime ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(token) ){
    			MarketUser user = getMarketUserByToken(token);
    			if( null != user ){
    				Map<String, Object> dataMap = new HashMap<String,Object>();
    				
    				ProfitMoneyExample example = new ProfitMoneyExample();
    				example.setOrderByClause("CREATE_TIME DESC");
    				Criteria criteria = example.createCriteria();
    				criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    				criteria.andMarketUserIdEqualTo(user.getId());
    				
    				if( StringHelper.isNotBlank(name) ){
    					criteria.andUserNickNameLike( "%" + name + "%" );
    				}
    				
    				if( StringHelper.isNotBlank(startTime) ){
    					criteria.andCreateTimeGreaterThanOrEqualTo(DateHelper.getDateByStringDate(startTime+" 00:00:00"));
    				}
    				if( StringHelper.isNotBlank(endTime) ){
    					criteria.andCreateTimeLessThanOrEqualTo(DateHelper.getDateByStringDate(endTime+" 23:59:59"));
    				}
    				example = this.handlePageQuery(request, example);
    				int count = profitMoneyService.count(example);
    				List<ProfitMoney> list = profitMoneyService.list(example);
    				dataMap.put("orders", list);
    				dataMap.put("count", count);
    				resultMap.put(DATA, dataMap);
    				result = SUCCESS;
					errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG;
    	        	
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    /**
     * 预计收益明细
     * @return
     */
    @RequestMapping(value="/yjmx")
    @ResponseBody
    public Map<String,Object> yjmx( HttpServletRequest request,HttpServletResponse response, String token, String isGive, String keyword,String orderNum, String startTime, String endTime ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(token) ){
    			MarketUser user = getMarketUserByToken(token);
    			if( null != user ){
    				Map<String, Object> dataMap = new HashMap<String,Object>();
    				
    				PreMoneyExample example = new PreMoneyExample();
    				example.setOrderByClause("ORDER_DATE DESC");
    				com.itic.market.system.premoney.persistence.model.PreMoneyExample.Criteria criteria = example.createCriteria();
    				criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    				criteria.andMarketUserIdEqualTo(user.getId());
    				if( StringHelper.isNotBlank(isGive) ){
    					criteria.andIsGiveEqualTo(isGive);
    				}
    				if( StringHelper.isNotBlank(keyword) ){
    					criteria.andNoteLike( "%" + keyword + "%" );
    				}
    				
    				if( StringHelper.isNotBlank(startTime) ){
    					criteria.andCreateTimeGreaterThanOrEqualTo(DateHelper.getDateByStringDate(startTime+" 00:00:00"));
    				}
    				if( StringHelper.isNotBlank(endTime) ){
    					criteria.andCreateTimeLessThanOrEqualTo(DateHelper.getDateByStringDate(endTime+" 23:59:59"));
    				}
    				if( StringHelper.isNotBlank(orderNum) ){
    					criteria.andOrderNumLike("%"+orderNum+"%");
    				}
    				example = this.handlePageQueryDefault(request, example);
    				int count = preMoneyService.count(example);
    				List<PreMoney> list = preMoneyService.list(example);
    				dataMap.put("orders", list);
    				dataMap.put("count", count);
    				
    				String totalPreMoney = "0";
    				String temp = preMoneyService.statisTotalPreMoney(user.getId());
    				if( StringHelper.isNotBlank(temp) ){
    					double x = Double.parseDouble(temp);
                		NumberFormat ddf1=NumberFormat.getNumberInstance() ;

                		ddf1.setMaximumFractionDigits(2); 
                		totalPreMoney= ddf1.format(x) ;
    				}
    				
    				if( "635027".equals(user.getInvitingCode()) ){
    					totalPreMoney = NumberStringHelper.addString(totalPreMoney, "171.02");
    				}
    				
    				if( "288154".equals(user.getInvitingCode()) ){
    					totalPreMoney = NumberStringHelper.addString(totalPreMoney, "157.43");
    				}
    				
    				if( "650111".equals(user.getInvitingCode()) ){
    					totalPreMoney = NumberStringHelper.addString(totalPreMoney, "50.43");
    				}
    				
    				if( "476343".equals(user.getInvitingCode()) ){
    					totalPreMoney = NumberStringHelper.addString(totalPreMoney, "50.84");
    				}
    				
    				if( "542455".equals(user.getInvitingCode()) ){
    					totalPreMoney = NumberStringHelper.addString(totalPreMoney, "17.58");
    				}
    				
    				
    				dataMap.put("money", totalPreMoney);//未结算收益
    				
    				dataMap.put("totalMoney", user.getPreProfit());//总预计收益
    				
    				resultMap.put(DATA, dataMap);
    				result = SUCCESS;
					errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG;
    	        	
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    

    /**
     * APP用户注册成商家
     * @return
     */
    @RequestMapping(value="/phone")
    @ResponseBody
    public Map<String,Object> phone(HttpServletRequest request,HttpServletResponse response,String token,String phone,String code){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(phone) && StringHelper.isNotBlank(code) ){
    			MarketUser user = getMarketUserByToken(token);
    			if( null != user ){
    				//判断用户的手机号码与验证码是否有效
    				if( isPhoneCode(phone, code) ){
    					//更新用户的手机号码字段 
    					user.setUserPhone(phone);
    					result = marketUserService.edit(user);
    					errorCode = SUCCESS_ERROR_CODE;
        	        	errorMsg = SUCCESS_ERROR_MSG;
        	        	
    				}else{
    					errorCode = PHONE_CODE_ERROR_CODE;
        	        	errorMsg = PHONE_CODE_ERROR_MSG;
    				}
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    
    /**
     * APP用户注册成商家
     * @return
     */
    @RequestMapping(value="/name")
    @ResponseBody
    public Map<String,Object> name(HttpServletRequest request,HttpServletResponse response,String token,String name){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(name) ){
    			MarketUser user = getMarketUserByToken(token);
    			if( null != user ){
    				user.setUserNickName(name);
    				result = marketUserService.edit(user);
					errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG;
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
}